import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/page/dashboard_page.dart';
import 'package:finance_app/src/page/login/create_account_page.dart';
import 'package:finance_app/src/page/login/create_business_profile_page.dart';
import 'package:finance_app/src/page/login/login_page.dart';
import 'package:finance_app/src/page/login/pending_invitation_page.dart';
import 'package:finance_app/src/page/splash_second_page.dart';
import 'package:finance_app/src/provider/dashboard_provider.dart';
import 'package:finance_app/src/provider/login/create_account_provider.dart';
import 'package:finance_app/src/provider/login/create_business_profile_provider.dart';
import 'package:finance_app/src/provider/login/login_provider.dart';
import 'package:finance_app/src/provider/login/pending_invitation_provider.dart';
import 'package:finance_app/src/provider/splash_second_provider.dart';
import 'package:flutter/foundation.dart';

class OverrideRouteProviders extends IRoutesProvider {
  static final indexPage = '/home';

  @override
  void registerRoutes(RRouter router) {
    if (!Config.notFirst) {
      router.addPRouter<SplashSecondProvider>(
        path: indexPage,
        routerWidgetBuilder: (p) => SplashSecondPage(),
        providerBuilder: (p) => SplashSecondProvider(),
        isReplaceRouter: true,
      );
    } else if (Config.token != null) {
      if (Config.myInvitations != null &&
          Config.myInvitations.length > 0 &&
          kDebugMode == false) {
        router.addPRouter<PendingInvitationProvider>(
          path: indexPage,
          routerWidgetBuilder: (p) => PendingInvitationPage(),
          providerBuilder: (p) => PendingInvitationProvider(),
          isReplaceRouter: true,
        );
      } else if (Config.userProfile?.name == null) {
        router.addPRouter<CreateAccountProvider>(
          path: indexPage,
          routerWidgetBuilder: (p) => CreateAccountPage(),
          providerBuilder: (p) => CreateAccountProvider(),
          isReplaceRouter: true,
        );
      } else if (Config.userProfile?.defaultCompanyId == null) {
        router.addPRouter<CreateBusinessProfileProvider>(
          path: indexPage,
          routerWidgetBuilder: (p) => CreateBusinessProfilePage(),
          providerBuilder: (p) => CreateBusinessProfileProvider(),
          isReplaceRouter: true,
        );
      } else {
        router.addPRouter<DashboardProvider>(
          path: indexPage,
          routerWidgetBuilder: (p) => DashboardPage(),
          providerBuilder: (p) => DashboardProvider(),
          isReplaceRouter: true,
        );
      }
    } else {
      router.addPRouter<LoginProvider>(
        path: indexPage,
        routerWidgetBuilder: (p) => LoginPage(
          toastMessage: p != null && p["toastMessage"] != null
              ? p["toastMessage"] as String
              : null,
        ),
        providerBuilder: (p) => LoginProvider(),
        isReplaceRouter: true,
      );
    }
  }
}
